//================v1要点总结===============
/**
 * 1. 深度遍历统计个数，遍历过的都赋值为0
 */

/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxAreaOfIsland = function (grid) {

    // 查找，统计，赋值为0 
    let area = 0

    let r = grid.length
    if (!r) {
        return area
    }
    let c = grid[0].length
    if (!c) {
        return area
    }

    let dirR = [1, 0, -1, 0]
    let dirC = [0, 1, 0, -1]


    function dfs(ri, ci, tempArea) {
        tempArea[0]++
        grid[ri][ci] = 0
        for (let i = 0; i < dirR.length; i++) {
            let tempR = ri + dirR[i]
            let tempC = ci + dirC[i]
            if (tempR >= 0 && tempR < r && tempC >= 0 && tempC < c && grid[tempR][tempC]) {
                dfs(tempR, tempC, tempArea)
            }
        }
    }

    for (let i = 0; i < r; i++) {
        for (let j = 0; j < c; j++) {
            if (grid[i][j]) {
                // 开始统计面积，边统计，边赋值为0
                let tempArea = [0]
                dfs(i, j, tempArea)
                area = Math.max(area, tempArea[0])
            }
        }
    }
    return area
};


let input = [
    [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
    [0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0]
]


console.log(maxAreaOfIsland(input))